package com.hnit.configs;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "alipay")
@Data
public class AlipayConfig {
    private String appId;
    private String privateKey;
    private String publicKey;
    private String gatewayUrl;
    private String notifyUrl;
    private String signType;
    private String charset = "UTF-8";

    // 可以在配置文件中设置超时时间，这里设置默认值
    private int connectTimeout = 60000; // 连接超时时间，默认30秒
    private int readTimeout = 60000;    // 读取超时时间，默认30秒

    @Bean
    public AlipayClient alipayClient() {
        // 先实例化DefaultAlipayClient对象
        DefaultAlipayClient alipayClient = new DefaultAlipayClient(
                gatewayUrl,
                appId,
                privateKey,
                "json",
                "UTF-8",
                publicKey,
                "RSA2"
        );

        // 设置超时时间（单位：毫秒）
        alipayClient.setConnectTimeout(connectTimeout);
        alipayClient.setReadTimeout(readTimeout);

        return alipayClient;
    }
}
